<template>
  <div class="mine-page">
    <!-- 概览卡片 -->
    <el-row :gutter="20" class="overview-cards">
      <el-col :span="6">
        <el-card class="overview-card balance-card">
          <div class="card-content">
            <div class="card-icon">
              <el-icon size="30"><Wallet /></el-icon>
            </div>
            <div class="card-info">
              <div class="card-title">账户余额</div>
              <div class="card-value">¥{{ formatNumber(balance) }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="overview-card income-card">
          <div class="card-content">
            <div class="card-icon">
              <el-icon size="30"><TrendCharts /></el-icon>
            </div>
            <div class="card-info">
              <div class="card-title">本日收入</div>
              <div class="card-value">¥{{ formatNumber(todayIncome) }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="overview-card month-income-card">
          <div class="card-content">
            <div class="card-icon">
              <el-icon size="30"><Histogram /></el-icon>
            </div>
            <div class="card-info">
              <div class="card-title">本月收入</div>
              <div class="card-value">¥{{ formatNumber(monthIncome) }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="overview-card total-income-card">
          <div class="card-content">
            <div class="card-icon">
              <el-icon size="30"><Coin /></el-icon>
            </div>
            <div class="card-info">
              <div class="card-title">累计收入</div>
              <div class="card-value">¥{{ formatNumber(totalIncome) }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 图表和操作区域 -->
    <el-row :gutter="20" class="content-section">
      <el-col :span="18">
        <el-card class="chart-card">
          <template #header>
            <div class="chart-header">
              <span>收入统计</span>
              <div class="chart-controls">
                <el-radio-group v-model="chartType" size="small">
                  <el-radio-button label="week">近7天</el-radio-button>
                  <el-radio-button label="month">近30天</el-radio-button>
                  <el-radio-button label="year">近1年</el-radio-button>
                </el-radio-group>
              </div>
            </div>
          </template>
          <div class="chart-container">
            <div ref="chartRef" class="chart"></div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="action-card">
          <template #header>
            <div class="action-header">
              <span>快捷操作</span>
            </div>
          </template>
          <div class="action-content">
            <el-row :gutter="15">
              <el-col :span="12">
                <el-button type="primary" plain class="action-button">
                  <el-icon><Promotion /></el-icon>
                  <div>提现</div>
                </el-button>
              </el-col>
              <el-col :span="12">
                <el-button type="success" plain class="action-button">
                  <el-icon><Document /></el-icon>
                  <div>账单</div>
                </el-button>
              </el-col>
              <el-col :span="12">
                <el-button type="warning" plain class="action-button">
                  <el-icon><DataAnalysis /></el-icon>
                  <div>分析</div>
                </el-button>
              </el-col>
              <el-col :span="12">
                <el-button type="info" plain class="action-button">
                  <el-icon><Setting /></el-icon>
                  <div>设置</div>
                </el-button>
              </el-col>
            </el-row>
          </div>
        </el-card>
        
        <el-card class="recent-card">
          <template #header>
            <div class="recent-header">
              <span>最近收入</span>
              <el-button type="text" @click="viewAllIncome">查看全部</el-button>
            </div>
          </template>
          <div class="recent-content">
            <el-table :data="recentIncome" style="width: 100%" :show-header="false">
              <el-table-column prop="date" width="90"></el-table-column>
              <el-table-column prop="description"></el-table-column>
              <el-table-column prop="amount" align="right" width="90">
                <template #default="scope">
                  <span class="income-amount">+{{ scope.row.amount }}</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang='ts'>
import { ref, onMounted, watch } from 'vue'
import * as echarts from 'echarts'
import { 
  Wallet, 
  TrendCharts, 
  Histogram, 
  Coin, 
  Promotion, 
  Document, 
  DataAnalysis, 
  Setting 
} from '@element-plus/icons-vue'

// 数据定义
const balance = ref(12865.80)
const todayIncome = ref(320.50)
const monthIncome = ref(8650.20)
const totalIncome = ref(125680.50)
const chartType = ref('week')
const chartRef = ref<HTMLDivElement | null>(null)
let chart: echarts.ECharts | null = null

// 最近收入数据
const recentIncome = ref([
  { date: '今天', description: '商品销售', amount: '120.00' },
  { date: '昨天', description: '服务费用', amount: '85.50' },
  { date: '前天', description: '推广佣金', amount: '210.30' },
  { date: '前天', description: '推广佣金', amount: '210.30' },
  // { date: '前天', description: '推广佣金', amount: '210.30' },
])

// 图表数据
const chartData = ref({
  week: {
    dates: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    values: [120, 132, 101, 134, 90, 230, 210]
  },
  month: {
    dates: Array.from({ length: 30 }, (_, i) => `${i + 1}日`),
    values: Array.from({ length: 30 }, () => Math.floor(Math.random() * 300 + 100))
  },
  year: {
    dates: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
    values: [820, 932, 901, 934, 1290, 1330, 1320, 1200, 1100, 1400, 1500, 1600]
  }
})

// 格式化数字
const formatNumber = (num: number): string => {
  return num.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
}

// 初始化图表
const initChart = () => {
  if (chartRef.value) {
    chart = echarts.init(chartRef.value)
    updateChart()
  }
}

// 更新图表
const updateChart = () => {
  if (!chart) return
  
  const option = {
    tooltip: {
      trigger: 'axis',
      formatter: '{b}<br/>{a}: ¥{c}'
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: chartData.value[chartType.value as keyof typeof chartData.value].dates,
      boundaryGap: false
    },
    yAxis: {
      type: 'value',
      axisLabel: {
        formatter: '¥{value}'
      }
    },
    series: [{
      name: '收入',
      type: 'line',
      data: chartData.value[chartType.value as keyof typeof chartData.value].values,
      smooth: true,
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: 'rgba(64, 158, 255, 0.3)' },
          { offset: 1, color: 'rgba(64, 158, 255, 0.01)' }
        ])
      },
      itemStyle: {
        color: '#409EFF'
      },
      lineStyle: {
        width: 2
      }
    }]
  }
  
  chart.setOption(option)
}

// 查看全部收入
const viewAllIncome = () => {
  console.log('查看全部收入')
}

// 监听图表类型变化
watch(chartType, () => {
  updateChart()
})

// 组件挂载后初始化
onMounted(() => {
  initChart()
  window.addEventListener('resize', () => {
    if (chart) {
      chart.resize()
    }
  })
})
</script>

<style lang='scss' scoped>
.mine-page {
  padding: 20px;
  width: 98%;
  box-sizing: border-box;
  margin: 0 auto; /* 居中显示 */
  overflow: hidden;
}

.overview-cards {
  margin-bottom: 20px;
}

.overview-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  border: none;
  
  .card-content {
    display: flex;
    align-items: center;
    padding: 20px;
  }
  
  .card-icon {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 15px;
    flex-shrink: 0;
  }
  
  .card-info {
    flex: 1;
  }
  
  .card-title {
    font-size: 14px;
    color: #909399;
    margin-bottom: 5px;
  }
  
  .card-value {
    font-size: 22px;
    font-weight: bold;
    margin-bottom: 5px;
  }
  
  .card-trend {
    .trend {
      font-size: 12px;
      color: #67c23a;
      
      &.trend-up {
        color: #67c23a;
      }
      
      .el-icon {
        vertical-align: middle;
        margin-right: 2px;
      }
    }
  }
}

.balance-card {
  .card-icon {
    background-color: rgba(102, 126, 234, 0.1);
    color: #667eea;
  }
  
  .card-value {
    color: #667eea;
  }
}

.income-card {
  .card-icon {
    background-color: rgba(245, 87, 108, 0.1);
    color: #f5576c;
  }
  
  .card-value {
    color: #f5576c;
  }
}

.month-income-card {
  .card-icon {
    background-color: rgba(79, 172, 254, 0.1);
    color: #4facfe;
  }
  
  .card-value {
    color: #4facfe;
  }
}

.total-income-card {
  .card-icon {
    background-color: rgba(255, 184, 0, 0.1);
    color: #ffb800;
  }
  
  .card-value {
    color: #ffb800;
  }
}

.content-section {
  .chart-card {
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
    height: 575px;
    border: none;
    .chart-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    
    .chart-controls {
      .el-radio-group {
        :deep(.el-radio-button__inner) {
          border-radius: 4px;
          margin-left: 10px;
        }
      }
    }
    
    .chart-container {
      height: 400px;
      padding: 10px 0;
      
      .chart {
        width: 100%;
        height: 100%;
      }
    }
  }
  
  .action-card {
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
    border: none;
    margin-bottom: 20px;
    
    .action-content {
      padding: 10px 0;
      
      .action-button {
        width: 100%;
        height: 80px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        margin-bottom: 10px;
        font-size: 14px;
        
        .el-icon {
          margin-right: 0;
          margin-right: 3px;
          font-size: 20px;
        }
      }
    }
  }
  
  .recent-card {
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
    border: none;
    
    .recent-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    
    .recent-content {
      .income-amount {
        color: #67c23a;
        font-weight: 500;
      }
    }
  }
}

:deep(.el-card__header) {
  border-bottom: 1px solid #ebeef5;
  padding: 15px 20px;
  font-weight: 500;
}

@media (max-width: 1200px) {
  .overview-cards {
    .el-col {
      margin-bottom: 15px;
    }
  }
  
  .content-section {
    .chart-card {
      .chart-container {
        height: 300px;
      }
    }
  }
}
</style>